<?php
// +----------------------------------------------------------------------
// | Author: weifashi <605403358@qq.com>
// +----------------------------------------------------------------------
namespace app\admin\actions;
use app\admin\actions\controls\SearchService;
use app\admin\actions\controls\GridService;
use app\admin\actions\controls\FormService;
use app\admin\actions\Action;

class Controls    {
	
	//基础定义
	private $xml;
	private $nodeId;
		
	/**
    * 构造函数自动执行
    * @return null
    */
    public function __construct($xml=null,$nodeId=null){
		error_reporting(0);
    	$this ->xml =  $xml;
    	$this ->nodeId = $nodeId;
    }
	
	/**
     * 获取示例
     * @param array $options 实例配置
     * @return static
     */
    public static function instance($xml=null,$nodeId=null)
    {
        return new self($xml,$nodeId);
    }
	
    /**
    * grid获取选项配置
    * @access public
    * @param string $id   			xml-id
    * @param string $xml 			xml-路径
    * @param array  $isEach 	直接输出，还是返回数据
    * @return array
    */
	public function getGridConfig($isEach=true){
		$data = GridService::instance($this->xml,$this->nodeId)->getOption();
		//按钮权限控制
		$function = isset($data['function'])  ? $data['function'] : $xmlFile['function'].'';
		$data['btns'] =  Action::instance()->checkBtnQuth($function,$data['btns']);
		foreach($data['cols'] as $key => $cols){
			foreach($cols as $k => $col){
				if($col['buttons'][0]){
					$data['cols'][$key][$k]['buttons'] = 
					Action::instance()->checkBtnQuth($function,$col['buttons']);
				}
			}
		}
		//返回
		return $isEach ? \Api::ReSucess($data) : $data;
	} 
	
	
	/**
    * grid获取数据
    * @access public
    * @param string 			$pageSize   		每页数据条数
    * @param string 			$pageIndex 		指定第几页
    * @return array
    */
	public function getGridData($isEach=true){
		$data = GridService::instance()->getData();
		return $isEach ? \Api::ReSucess($data) : $data;
	}
	
	/**
    * grid删除数据
    * /admin/Api/Grid/deleteData
    * @access public
    * @param array $data   删除数据 [{id:1},{id:2}]
    * @return array
    */
	public function deleteGridData($data){
		$data = GridService::instance()->deleteData( json_decode($data,true));
		return \Api::ReSucess($data,"删除成功");
	}
	
	/**
    * grid调整排序
    * @access public
    * @param string $id   			xml-id
    * @param string $xml 			xml-路径
    * @return array
    */
	public function adjustSort($moveId,$coverId,$sortField){
		$data = GridService::instance()->adjustSort($moveId,$coverId,$sortField);
		return \Api::ReSucess($data,"删除成功");
	}
	
	/**
    * grid导出
    * @access public
    * @return array
    */
	public function exportExcel(){
		if (! function_exists('root_path')) {
		    function root_path($path = ''){
		        return env('root_path').($path ? ltrim($path, DIRECTORY_SEPARATOR) : 
		        ltrim($path, DIRECTORY_SEPARATOR));
		    }
		}
		$xls = GridService::instance()->getXlsData();
		require root_path().'/extend/base/files/PHPExeclCore.php';
		\PHPExeclCore::RecursionCreateExecl($xls['xlsName'],$xls['xlsHead'],$xls['xlsData']);
	}
	
	/**
    * grid更新字段
    * @access public
    * @param string $id   			xml-id
    * @param string $xml 			xml-路径
    * @return array
    */
	public function updateField($ids,$name,$value){
		GridService::instance()->updateField($ids,$name,$value);
		return \Api::ReSucess();
	}
	
	/**
    * grid更新行
    * @access public
    * @param string $id   			xml-id
    * @param string $xml 			xml-路径
    * @return array
    */
	public function updateRow($ids,$data){
		GridService::instance()->updateRow($ids,$data);
		return \Api::ReSucess();
	}
	
	
	/**
    * Search获取选项配置
    * @access public
    * @param string $id   			xml-id
    * @param string $xml 			xml-路径
    * @param array  $isEach 	直接输出，还是返回数据
    * @return array
    */
	public function getSearchConfig($isEach=true){
		$data = SearchService::instance($this->xml,$this->nodeId)->getOption();
		return $isEach ? \Api::ReSucess($data) : $data;
	}
	
	
	/**
    * Form获取选项配置
    * @access public
    * @param string $id   			xml-id
    * @param string $xml 			xml-路径
    * @param array  $isEach 	直接输出，还是返回数据
    * @return array
    */
	public function getFormConfig($isEach=true){
//		try {
			$data = FormService::instance($this->xml,$this->nodeId)->getOption();
		    $function = isset($data['function'])  ? $data['function'] : $xmlFile['function'].'';
		    $buttons = $data['buttons']['button'];
            $data['buttons']['button'] =  Action::instance()->checkBtnQuth($function,$buttons,'button');
			return $isEach ? \Api::ReSucess($data) : $data;
//		} catch (\Exception $e) {
//			return \Api::ReError($e->getMessage()  ?: '');	
//		}
	}
	
	
	/**
    * Form保存数据
    * @access public
    * @return array
    */
	public function saveFormData(){
		//令牌验证
//			$validate = new Validate;
//			$validate->rule(['__token__'=>'token']);
//	        if(!$validate->check (['__token__'=>request()->header()['token'] ]) ) {
//	        	return \Api::ReError($validate->getError());	
//	        }
		//保存
		$param = request()->param();
		$data = json_decode( $param["data"],true);
		$data = FormService::instance($this->xml,$data['id'])->saveData();
		
		//返回
		$msg = $param['mode'] != 1 ? "更新成功！" :  "新增成功！";
		return \Api::instance()->ReSucess($data['key'],$msg);
	}
}




